﻿using AntdUI;
using EONFULLSIN.Languages;
using EONFULLSIN.Models.Entity.User;
using EONFULLSIN.Utils;
using EONFULLSIN.Views.Table;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using Window = AntdUI.Window;

namespace EONFULLSIN.Views.UserView
{
    public partial class UserConfig : UserControl
    {
        private Window window;
        private List<User> userList;
        private User curUser;

        public UserConfig()
        {
            InitializeComponent();

            this.btn_add.Click += Btn_add_Click;
            this.btn_edit.Click += Btn_edit_Click;
            this.btn_del.Click += Btn_del_Click;
            this.btn_search.Click += Btn_search_Click;
            InitTableColumns();
            InitData();
            LanguageChangedEvent.LanguageChanged += UpdateLanguage;
            UpdateLanguage(LanguageHelper.CurrentLanguage);
        }

        private void UpdateLanguage(string obj)
        {
            pageHeader1.Text = LanguageHelper.GetTranslation(obj, "UserConfig_Title");
            groupBox1.Text = LanguageHelper.GetTranslation(obj, "UserConfig_GroupBox1");
            label1.Text = LanguageHelper.GetTranslation(obj, "UserConfig_Lablel1");
            txt_user.PlaceholderText = LanguageHelper.GetTranslation(obj, "Text_UserName");
            btn_search.Text = LanguageHelper.GetTranslation(obj, "UserConfig_Button_Search");
            btn_add.Text = LanguageHelper.GetTranslation(obj, "UserConfig_Button_Add");
            btn_edit.Text = LanguageHelper.GetTranslation(obj, "UserConfig_Button_Edit");
            btn_del.Text = LanguageHelper.GetTranslation(obj, "UserConfig_Button_Del");
            var UserColumn = tab_user.Columns;
            foreach (var item in UserColumn)
            {
                if (item.Key == "Name")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "UserConfig_table_Name");
                }
                if (item.Key == "Account")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "UserConfig_table_Account");
                }
                if (item.Key == "Rank")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "UserConfig_table_Rank");
                }
                if (item.Key == "LastLoginTime")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "UserConfig_table_LastLoginTime");
                }
                if (item.Key == "Status")
                {
                    item.Title = LanguageHelper.GetTranslation(obj, "UserConfig_table_Status");
                }
            }
            this.tab_user.Columns = UserColumn;
            tab_user.Refresh();
        }

        private void Btn_search_Click(object sender, EventArgs e)
        {
            InitData(txt_user.Text);
        }

        private void Btn_del_Click(object sender, EventArgs e)
        {
            foreach (var item in userList)
            {
                if (item.Selected)
                {
                    EFDAL.Instance.DeleteModel<User>(x => x.Id.Equals(item.Id));
                }
            }
            InitData();
        }

        private void Btn_edit_Click(object sender, EventArgs e)
        {
            foreach (var item in userList)
            {
                if (item.Selected)
                {
                    var form = new UserAdd(window, "编辑用户", item.Id) { Size = new Size(250, 260) };
                    AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
                    {
                        BtnHeight = 0,
                    });
                    InitData();
                }
            }
        }

        private void InitTableColumns()
        {
            this.tab_user.Columns = new ColumnCollection()
            {
                new ColumnCheck("Selected"){Fixed = true},
                new Column("Name", "名称", ColumnAlign.Center)
                {
                    Width="120",
                },
                new Column("Account", "账号",ColumnAlign.Center),
                new Column("Rank","角色等级"),
                new Column("LastLoginTime","最后登录时间"),
                new Column("Status","状态")
            };
        }

        private void InitData(string search = null)
        {
            userList = EFDAL.Instance.GetModelList<User>(x => !x.IsDeleted);
            if (search != null)
            {
                userList = userList.Where(x => x.Account.Contains(search)).ToList();
            }
            tab_user.DataSource = userList;
            tab_user.SetRowEnable(0, false, true);
        }

        private void Btn_add_Click(object sender, EventArgs e)
        {
            var form = new UserAdd(window, "新增用户") { Size = new Size(250, 260) };
            AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
            {
                BtnHeight = 0,
            });
            InitData();
        }
    }
}